import {computed, ref, watch} from 'vue' 
import store from '@/store'
import { getcompanys } from '@/api/requst/management'
const dayjs = require('dayjs');
// import { useStore } from 'vuex'
// const $store = useStore()


export function useRepairManagementQuery() {
 
  const repairManagementQuery=ref([
    {
        label: '报修单号',
        prop: 'repCode',
        type: 'input',
        defaultValue: '',
        inputProps: {
          clearable: true,
          placeholder: '请输入报修单号'
        }
    },
    {
        label: '报修单位',
        prop: 'createUserOrgId',
        type: 'treeSelect',
        defaultValue: '',
        inputProps: {
          'default-expand-all': true,
          data: []
        }
      },
      {
        label: '报修人',
        prop: 'createUserName',
        type: 'input',
        defaultValue: '',
        inputProps: {
          clearable: true,
          placeholder: '请输入报修人'
        }
    },
    {
        label: '报修时间',
        prop: 'repairtime',
        type: 'datePicker',
        inputProps: {
          type: 'datetimerange',
          'value-format': 'YYYY-MM-DD HH:mm:ss',
          "range-separator": "至",
          "start-placeholder":"YYYYMMDD HH:MM",
        "end-placeholder":"YYYYMMDD HH:MM",
        defaultTime: [
          new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 0, 0, 0),
          new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 23, 59, 59)
        ]
        },
 
      },

      {
        label: '航站楼',
        prop: 'terminal',
        type: 'checkbox',
        // span: 6,
        defaultValue: [],
        inputProps: {
          // 控制是否显示权限功能
          showCheckAll: false,
          options: [
          
          ]
        }
      },
      {
        label: '问题类型',
        prop: 'problemId',
        type: 'select',
        defaultValue: '',
        inputProps: {
          clearable: true,
          options: []
        }
      },
      {
        label: '设备类型',
        prop: 'eqptTypeId',
        type: 'treeSelect',
        defaultValue: '',
        inputProps: {
          'default-expand-all': true,
          clearable: true,
          data:[]
        }
      },
      {
        label: '问题归属',
        prop: 'organType',
        type: 'radio',
        defaultValue: [],
        inputProps: {
          // 控制是否显示权限功能
          showCheckAll: false,
          options: [
          {label:'内部单位',value:'1'},
          {label:'外部单位',value:'2'}
          ]
        }
      },
      {
        label: '受理单位',
        prop: 'organId',
        type: 'treeSelect',
        defaultValue: '',
        inputProps: {
          'default-expand-all': true,
          data: [
          ],
        }
      },
      {
        label: '紧急程度',
        prop: 'urgency',
        type: 'select',
        defaultValue: '',
        inputProps: {
          clearable: true,
          options: [         
              { label: '非常紧急', value: '1' },
              { label: '一般紧急', value: '2' },
              { label: '一般', value: '3' }
          ]
        }
      },
      {
        label: '状态',
        prop: 'repApplyState',
        type: 'select',
        defaultValue: '',
        inputProps: {
          clearable: true,
          options: [
            { label: '待分析', value: '01' },
            { label: '已受理', value: '02' },
            { label: '已驳回', value: '03' },
            { label: '已挂起', value: '04' },
          ]
        }
      },
      {
        label: '楼层',
        prop: 'eqptFloor',
        type: 'select',
        defaultValue: '',
        inputProps: {
          clearable: true,
          options: [
            // { label: '全部', value: 'all' }
          ]
        }
      },
  ])

  // 问题类型
  watch(() => store.getters['repair/getPrograms'], (newVal) => {
    // console.log(newVal)
    repairManagementQuery.value[5].inputProps.options = newVal
  })
//设备类型
  watch(()=>store.getters['repair/getEqptTypes'],(newVal)=>{
    newVal=[...newVal, { label: '其他', value: '0' }]
    repairManagementQuery.value[6].inputProps.data = newVal
    console.log(newVal,'newVal');
    
  })

  // 航站楼
  watch(()=>store.getters['repair/getTermianl'],(newVal)=>{

    repairManagementQuery.value[4].inputProps.options = newVal.map((item :any)=> {return {label:`${item.label}航站楼${item.children.length }`,value:item.value}})

    
    repairManagementQuery.value[11].inputProps.options = newVal[0].children
  })

 
  // 报修单位 & 接收单位
  watch(()=>store.getters['user/getAllOrganTree'],(newVal)=>{
    handleTree(newVal)
    repairManagementQuery.value[1].inputProps.data = newVal
 //接收单位
 watch(()=>store.getters['repair/getascription'],()=>{
  if(store.getters['repair/getascription']==='1'){
    repairManagementQuery.value[8].inputProps.data = newVal
  }else{
     //上报
     getcompanys().then((res: any) => {
      if (res.code === '1') {
        repairManagementQuery.value[8].inputProps.data = res.data.map((item: any) => ({
          ...item,
          label: item.companyName,
          value: item.companyId
        }))
      }
    })
    // repairManagementQuery.value[8].inputProps.data =[]
  }
})
  })




  const handleTree = (data:any) => {
    data.forEach((item:any) => {
      item.label = item.data.name
      item.value = item.data.organId
      if(item.children) {
        handleTree(item.children)
      }
    })
  }



  return {
    repairManagementQuery,
  }
}